<?php 
session_start();
include("../db_connect.php");
?>
<script>
var loader = '<div align="center"style="padding:10px; margin:auto"><img src="images/loading.gif" alt="Please Wait"/>loading.....</div>';

/*tab*/
$(function(){
    $("#tabs #tab").click(function() {
	var tab = $(this).attr("name");
	$.ajax({
	url:"scripts/tabseter.php?no="+tab,
	success:function(data){}
	
	});
	
  });
	
 $( "#tabs" ).tabs({ selected: <?php if(isset($_SESSION['tabadmin'] )){ echo $_SESSION['tabadmin']; } else{ echo $_SESSION['tabadmin'] = 0; } ?>


 });
 



});/*end of function*/

checked = false;		  
function checkedAllRequest(){
	if (checked == false){
		checked = true
	}
	else{
	checked = false
	}
	for (var i = 0; i < document.getElementById('myformRequest').elements.length; i++){
		document.getElementById('myformRequest').elements[i].checked = checked;
	}	
}



 $(document).ready(function() {
var brand = new String();
var model = new String();
var fname = new String();
var lname = new String();
 
$("#itemRequest").html(loader).load("admin/itemRequest.php");

 $("#brand").change(function () {
		model = $("#model").val();
		fname = $("#searchFname").val();
		lname = $("#searchLname").val();	
		brand = $(this).attr("value");
		var model2="model";
		if(brand == "select"){
			brand=="";
		}
		
		$("#model").empty().load("profile/selection.php?command="+model2 +"&modelid="+brand);
		$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
		
	});
	
$("#model").change(function () {
		model = $(this).attr("value");
		fname = $("#searchFname").val();
		lname = $("#searchLname").val();	
		brand = $("#brand").val();
		if(model == ""){
			model=="";
		}
		
		
			$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
			//$("#carsearch").html(loader).load("shop/carsearch_ajax.php?brand="+brand+"&model="+model+"&types="+types+"&conditions="+conditions+"&fuel="+fuel+"&transmission="+transmission);
});

$("#searchFname").keydown(function() {
		model = $("#model").val();
		fname = $(this).val();
		lname = $("#searchLname").val();	
		brand = $("#brand").val();
		
		if(fname == ""){
			fname =="";
		}

	$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
});
$("#searchLname").keydown(function() {
		model = $("#model").val();
		fname = $("#searchFname").val();
		lname = $(this).val();	
		brand = $("#brand").val();
		
		if(lname == ""){
			lname =="";
		}
	$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
});
	
	
	
	$("#requestTo").click(function(){

	var userdata = new Array();
	$(".itemRequest tr td").find("input:checked").each(function(e){
			userdata[e] = $(this).val();
		});
	
		if (userdata.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(userdata.length >=1){
		var disap = "approve";
		$("#msgbox").empty().html("Display this?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						approveRequest(userdata, disap);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//end of approve.click()
	
$("#requestTon").click(function(){

	var userdata = new Array();
	$("#itemRequest tr td").find("input:checked").each(function(e){
			userdata[e] = $(this).val();
		});
	
		if (userdata.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(userdata.length >=1){
		var disap = "disapprove";
		$("#msgbox").empty().html("Disapprove?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						approveRequest(userdata, disap);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//end of approve.click()
		
	
	
	
	
	
	
 $("#tbldashboard").hide();
	var on = true;
	$("#myaccount").on("click", function (){
		if(on){
		$("#tbldashboard").show();		
		on = false;
		}else{
		$("#tbldashboard").hide();
		on = true;
		}
		
	});
	
	
	
	
	$("#editAccount").click(function () {
		
		$("#fname").removeAttr('disabled');
		$("#lname").removeAttr('disabled');
		$("#email").removeAttr('disabled');
		$("#contact").removeAttr('disabled');
		$("#uname").removeAttr('disabled');
		$("#pass").removeAttr('disabled');
		$("#save").removeAttr('disabled');
	});

	$("#save").click(function () {
		var fname =	$("#fname");
		var lname =	$("#lname");
		var num = $("#contact");
		var	email = $("#email");
		var	uname = $("#uname");
		var	pass = $("#pass");
		
		if(fname.val() == "" || fname.val() == " "){
			alert("empty first name");
		}
		else if(lname.val() == "" || lname.val() == " "){
			alert("empty last name");
		}
		else if(num.val() == "" || num.val() == " "){
			alert("empty contact");
		}
		else if(isNaN(num.val())){
			alert("No. Only");
		}
		else if(email.val() == "" || email.val() == " "){
			alert("empty email");
		}
		else if(!isValidEmail( email.val() ) ){
			alert("input a valid email")
		}
		else if(uname.val() == "" || uname.val()== " "){
			alert("empty username");
		}
		else {
			checkusername(fname,lname,num,email,uname,pass) 
		}
	
		$("#itemRequest").load("itemRequest.php");
	
	});
 
 });/*end of document*/
 
function checkusername(fname, lname,  num, email,  uname, pass){
	
	if(uname.val() == "" || uname.val()== " "){
		alert("empty username");
	}
	else{
		$.ajax ({
				url:"scripts/isusername_exist.php?username="+uname.val(),
					success:function(data){
						if(data == 1){
							if(confirm("This is your current username >" +uname.val()+ "< do you want to change it?")){
							
							}
							else{
							//dapat merun comfirmation ng password eh tinamad ako haha saka ko na gagawin
							
								update(fname, lname,  num, email,  uname, pass);
							}
						}
						else{
							update(fname, lname,  num, email,  uname, pass);
						
						}
				}
			});
	}
}
/*for the editing of the admin account*/
function update(fname, lname,  num, email,  uname, pass){

	$.ajax({
	url:"admin/update.php?fname="+fname.val()+"&lname="+lname.val()+"&num="+num.val()+"& email="+email.val()+"&uname="+uname.val()+"&pass="+pass.val(),
	success:function(data){
		if(data=="1"){
			//$("#msgbox").html(data);
			//$("#msgbox").dialog():
			alert("Successfuly updated");
			location.reload();
		}
		else{
			//$("#msgbox").html(data);
			//alert("NO affected row");
			location.reload();
			
		}
	
	}
	
	});
		
	
}
/*just checking for a valid email format*/
function isValidEmail(strEmail){
var validRegExp;
validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1){
		return false;
    } 
    return true; 
}	
 
 function approveRequest(itemRequest,app){
	
	if(app =="approve"){
		$.ajax({
			url:"admin/approveitemrequest.php?itemRequest="+itemRequest+"&approve="+app,
			success:function(data){
			
			if(data){
					//$("msgbox").empty().html("");
					$("#msgbox").html("Sucessfully approved");
						$("#msgbox").dialog({
							modal:true,
							title:"",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( "#msgbox" ).dialog( "close" );
									location.reload();
									
								}

							},
						});
				}
				else{
					$("msgbox").empty().html("something went wrong");
						$("#msgbox").dialog({
							modal:true,
							title:"Error",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( "#msgbox" ).dialog( "close" );
								}

							},
						});
				
				}
			
			
			}


		});

	}else{
	
	$.ajax({
			url:"admin/approveitemrequest.php?itemRequest="+itemRequest+"&approve="+app,
			success:function(data){
			
			
				if(data){
					//$("msgbox").empty().html("");
					$("#msgbox").html("Sucessfully disapproved");
						$("#msgbox").dialog({
							modal:true,
							title:"",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( "#msgbox" ).dialog( "close" );
									$(".itemRequest").html(loader).load("admin/approveitemrequest.php");
								}

							},
						});
				}
				else{
					$("msgbox").empty().html("something went wrong");
						$("#msgbox").dialog({
							modal:true,
							title:"Error",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( "#msgbox" ).dialog( "close" );
								}

							},
						});
				
				}
			
			
			}


		});
	
	
	}

}
 
</script>

<div id="tabs">
					<ul>
						<li><a href="#tabs1" id="tab" name="0" >Profile</a></li>
						<li><a href="#tabs2" id="tab" name="1" >Messages</a></li>
						<li><a href="#tabs3" id="tab" name="2" >Orders</a></li>
					</ul>

					<div id="tabs1">
						<div id="dashboard">
								<h3>Welcome - <a href="#" style="color:red" id="dashname"><?php echo $_SESSION['fname'];?></a></h3>
										<?php
											$sql = "Select * from tbl_account where id=".$_SESSION['id']." ";
											$result = mysql_query($sql);
											while($row = mysql_fetch_array($result)){
											$fname = $row['fname'];
											$lname = $row['lname'];
											$email = $row['email'];
											$contact = $row['contacts'];
											$password = $row['password'];
											$username = $row['username'];
											}
										?>
										<!--part of admin.php-->
											<table>
												<tr>
													<td><a href="#" id="myaccount" style="font-size:20px; color:red;" >My Account</a>
													<div class="myprofile" style="padding-left:10px; font-size:18px;">
													<table id="tbldashboard">
															<tr>
																<td>First Name:</td>
																<td><input type="text" id="fname" value="<?php echo $fname?>" disabled/> </td>
															</tr>
															<tr>
																<td>Last Name:</td>
																<td><input type="text" id="lname" value="<?php echo $lname?>" disabled/> </td>
															</tr>		
															<tr>
																<td>Contact no:</td>
																<td>
																	<input type="text" id="contact" value="<?php echo $contact;?>" disabled/>
																	
																</td>
															</tr>
															<tr>
																<td>Email:</td>
																
																<td>
																<input type="text" id="email" value="<?php echo $email;?>" disabled/>
																</td>
															</tr>
															<tr>
																<td>Username:
																</td>
																<td>
																	<input type="text" id="uname" value="<?php echo $username;?>" disabled/>
																</td>
															</tr>
															<tr>
																<td>Password:
																</td>
																<td>
																	<input type="password" id="pass" value="<?php echo $password;?>" disabled/>
																</td>
															</tr>
															<tr>
																<td colspan="2">
																	<button id="editAccount" >Edit</button>
																	<button id="save" disabled>Save</button>
																</td>
															</tr>
															
														</table>
																	
													
													
													
														</div>
													</td>
												</tr>
											
											
											
											
											</table>
						</div>
					
					
					</div><!--end of tab1-->

					<div id="tabs2">
						<table>
							check mail;
						</table>
					</div>
					<div id="tabs3">
						<div id="itemlist">
							<div id="searchbox">
							<table>
								<tr>
									<td><label>Postedby:</label></td>
									<td><input type="text" id="searchFname"/></td>
									<td><label>LName:</label></td>
									<td><input type="text" id="searchLname"/></td>
									<td><label>Brand:</label></td>
									<td>
										<select id="brand" value="brand" name="brand">
										<?php 
											$sql = mysql_query("SELECT * FROM tblbrand");
											$opt = '<option id="optbrand" name="none" value="all">Select All</option> ';
											while($row = mysql_fetch_array($sql) ){
												$opt .= '<option id="optbrand" name="'.$row["brand"].'" value="'.$row["id"].'" >'.$row["brand"].'</option>';
											}
											echo $opt;
										?>
									</select>
									</td>
									<td><label>Model:</label></td>
									<td>
										<select id="model" value="">
										</select>
									</td>
									
									
								</tr>
								<tr>
									
									<td colspan="8"><label>Action:</label><button id="requestTo" name="display">Display</button> <button id="requestTon" name="out">Out</button></td>
									
								</tr>
								
							</table>
							
									
									
							</div>
							<div id="itemRequest" class="itemRequest">
								<?php //include("itemRequest.php"); ?>
							</div>
						</div>
					</div>
				</div>
				<!--end of tab-->
				